if not Tubes.FileTypes then
	Tubes.FileTypes = {}
end
local FileTypes = Tubes.FileTypes
FileTypes.Openers = {}

local folderTypes = {
	["cpuchip"] = "CPU code",
	["expression2"] = "Expression 2 code",
	["gpuchip"] = "GPU code"
}

function FileTypes.GetFileType (file)
	if file:IsFolder () then
		return "Folder"
	end
	local Folder = file:GetParent ()
	while Folder do
		if folderTypes [Folder:GetNiceName ():lower ()] then
			return folderTypes [Folder:GetNiceName ():lower ()]
		end
		Folder = Folder:GetParent ()
	end
	return "Unknown"
end

function FileTypes.GetOpener (fileType)
	print ("Opener: " .. fileType)
	return FileTypes.Openers [fileType]
end

function FileTypes.RegisterOpener (fileType, openerFunction)
	FileTypes.Openers [fileType] = openerFunction
end

FileTypes.RegisterOpener ("CPU code", function (file)
	file:Read (LocalPlayer (), function (code)
		wire_cpu_OpenEditor ()
		if file:IsReal () and
			file:GetRealPath ():sub (1, 5):lower () == "data/" then
			CPU_Editor:LoadFile (file:GetRealPath ():sub (6))
			return
		end
		local Sheet = CPU_Editor:CreateTab ("*" .. file:GetNiceName ())
		CPU_Editor:SetActiveTab (Sheet.Tab)
		CPU_Editor:ChosenFile ()
		CPU_Editor:SetCode (code)
	end)
end)

FileTypes.RegisterOpener ("Expression 2 code", function (file)
	file:Read (LocalPlayer (), function (code)
		openE2Editor ()
		if file:IsReal () and
			file:GetRealPath ():sub (1, 5):lower () == "data/" then
			wire_expression2_editor:LoadFile (file:GetRealPath ():sub (6))
			return
		end
		local Sheet = wire_expression2_editor:CreateTab ("*" .. file:GetNiceName ())
		wire_expression2_editor:SetActiveTab (Sheet.Tab)
		wire_expression2_editor:ChosenFile ()
		wire_expression2_editor:SetCode (code)
	end)
end)

FileTypes.RegisterOpener ("GPU code", function (file)
	file:Read (LocalPlayer (), function (code)
		wire_gpu_OpenEditor ()
		if file:IsReal () and
			file:GetRealPath ():sub (1, 5):lower () == "data/" then
			GPU_Editor:LoadFile (file:GetRealPath ():sub (6))
			return
		end
		local Sheet = GPU_Editor:CreateTab ("*" .. file:GetNiceName ())
		GPU_Editor:SetActiveTab (Sheet.Tab)
		GPU_Editor:ChosenFile ()
		GPU_Editor:SetCode (code)
	end)
end)